package com.ruoyi.common.utils.sendSms;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.models.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @ClassName SendSms
 * @Description 发送短信工具类
 * @Author luguanqing
 * @Date 2021/4/22 11:08
 * @Version 1.0
 */
@Component
public class SendSms {


    @Value("${aliyun.accessKeyId}")
    private static String accessKeyId;

    @Value("${aliyun.accessKeySecret}")
    private static String accessKeySecret;


    /**
     * 使用AK&SK初始化账号Client
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId("LTAI5t8YFuZwG5Y59PnJVcka")
                // 您的AccessKey Secret
                .setAccessKeySecret("CIR991kaZDSywxB9eKA8FlUrEn9qzL");
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    public String sendSms(Sms sms) throws Exception{
        com.aliyun.dysmsapi20170525.Client client = SendSms.createClient();
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        sendSmsRequest.setSignName(sms.getSingnName());
        sendSmsRequest.setTemplateCode(sms.getTemplateCode());
        HashMap<String, Object> paramMap = new HashMap<String, Object>(16);
        paramMap.put("code", sms.getCode());
        JSONObject json = new JSONObject(paramMap);
        sendSmsRequest.setTemplateParam(JSONObject.toJSONString(json));
        sendSmsRequest.setPhoneNumbers(sms.getPhoneNumbers());
        SendSmsResponse response = client.sendSms(sendSmsRequest);
        return response.getBody().getMessage();
    }

//    public static void main(String[] args_) throws Exception {
//        java.util.List<String> args = java.util.Arrays.asList(args_);
//        com.aliyun.dysmsapi20170525.Client client = SendSms.createClient("LTAI5t8YFuZwG5Y59PnJVcka", "CIR991kaZDSywxB9eKA8FlUrEn9qzL");
//        SendSmsRequest sendSmsRequest = new SendSmsRequest();
//        sendSmsRequest.setSignName("徐州安评应急培训有限公司");
//        sendSmsRequest.setTemplateCode("SMS_214620012");
//        HashMap<String, Object> paramMap = new HashMap<String, Object>(16);
//        paramMap.put("code", "2345");
//        JSONObject json = new JSONObject(paramMap);
//        sendSmsRequest.setTemplateParam(JSONObject.toJSONString(json));
//        sendSmsRequest.setPhoneNumbers("15605205636");
//        SendSmsResponse response = client.sendSms(sendSmsRequest);
//        System.out.println(response.getBody().getMessage());
//    }
}
